#include <math.h>
#include <iostream>
using namespace std;

#define TRANSFORM_FORWARD 1
#define TRANSFORM_INVERSE 0
#define MATH_PI 3.141592653589793238462643383279502

int transform(float *TransformMatrix, int *Matrix, int *Return, int Direction)
{
	int Temp[8][8];
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			int k = 0;
			for (int n = 0; n < 8; n++)
			{
				if (Direction == TRANSFORM_FORWARD)
					k = k + (int) (*(TransformMatrix + 8 * i + n) * (*(Matrix + 8 * n + j)));

				if (Direction == TRANSFORM_INVERSE)
					k = k + (int) (*(TransformMatrix + 8 * n + i) * (*(Matrix + 8 * n + j)));
			}
			Temp[i][j] = k;
		}
	}
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
		{
			int k = 0;
			for (int n = 0; n < 8; n++)
			{
				if (Direction == TRANSFORM_FORWARD)
					k = k + (int) (Temp[i][n] * (*(TransformMatrix + 8 * n + j)));
				if (Direction == TRANSFORM_INVERSE)
					k = k + (int) (Temp[i][n] * (*(TransformMatrix + 8 * j + n)));
			}
			*(Return + 8 * i + j) = k;
		}
	}
	return 1;
}

void main(int nVars, char* szVars[])
{
	float Array[8][8];
	// Making transform array
	for (int i = 0;i<8;i++)
	{
		for (int j=0;j<8;j++)
		{
			float k = 0;
			if (j == 0)
				k = 1 / sqrt((float)8);
			else
				k = 0.5;
			Array[i][j] = k * cos((float)(MATH_PI * (2 * i + 1) * j / 16));
//			cout << Array[i][j] << "\t";
		}
//		cout << "\n";
	}
	// Making an data array
	int Array2[8][8];
	for (int i = 0; i<8;i++)
	{
		for (int j = 0; j < 8; j++)
		{
			Array2[i][j] = 576 + 20 * i -17 * j;
			cout << Array2[i][j]<< "\t";
		}
		cout << "\n";
	}
	cout << "\n";
	int Result[8][8];
	int Result2[8][8];
	
	transform(&Array[0][0], &Array2[0][0], &Result[0][0], TRANSFORM_FORWARD);
	
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
			{
				cout << Result[i][j] << "\t";
		}
		cout << "\n";
	}
	cout << "\n";
	transform(&Array[0][0], &Result[0][0], &Result2[0][0], TRANSFORM_INVERSE);

	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
			{
				cout << Result2[i][j] << "\t";
		}
		cout << "\n";
	}
	cout << "\n";
	for (int i = 0; i < 8; i++)
	{
		for (int j = 0; j < 8; j++)
			{
				cout << Result2[i][j] - Array2[i][j] << "\t";
		}
		cout << "\n";
	}
	cout << "\n";
}